package com.qing.study.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${swagger2.title}")
    private String title;

    @Value("${swagger2.description}")
    private String description;

    @Value("${swagger2.contact.name}")
    private String contactName;

    @Value("${swagger2.contact.url}")
    private String contactUrl;

    @Value("${swagger2.contact.email}")
    private String contactEmail;

    @Value("${swagger2.version}")
    private String version;

    //创建文档说明
    public ApiInfo createAPI(){
        return new ApiInfoBuilder().title(title).
                description(description).
                contact(new Contact(contactName,contactUrl,contactEmail))
                .version(version)
                .build();
    }

    //创建Swagger扫描信息
    @Bean
    public Docket createD(Environment environment){
        //设置要显示swagger的环境
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);

        //添加header参数配置end
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(createAPI())
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qing.study.controller"))
                .build();
    }

    /**
     * any() // 任何请求都扫描
     * none() // 任何请求都不扫描
     * regex(final String pathRegex) // 通过正则表达式控制
     * ant(final String antPattern) // 通过ant()控制
     */
   @Bean
   public Docket createLoginGroupRestApi(Environment environment){
       //设置要显示swagger的环境
       Profiles profiles = Profiles.of("dev","test");
       boolean flag = environment.acceptsProfiles(profiles);
       List<Predicate<RequestHandler>> list = new ArrayList<>();
       Predicate<RequestHandler> user = RequestHandlerSelectors.basePackage("com.xx.study.controller");
       list.add(user);
       Predicate<RequestHandler> apiOperation = RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class);
       return new Docket(DocumentationType.SWAGGER_2)
               .groupName("用户相关分组")
               .useDefaultResponseMessages(false)
               .apiInfo(createAPI())
               .enable(flag)
               .select()
               .apis(Predicates.and(Predicates.or(list), apiOperation))
               .paths(PathSelectors.ant("/user/**"))
               .build();
   }
}
